#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
using namespace std;
void gotoxy(short x, short y) 
{
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void taomap(int A[][80])
{
	for(int i=0;i<28;i++)
	{	
		if((i==0)||(i==23)||(i==27)	)
			for(int j=0;j<80;j++)	
				{
					A[i][j]=219;
					printf("%c",A[i][j]);
				}
		else
			{
				for(int j=0;j<80;j++)
					{
					if((j==0)||(j==79))
					{	
						A[i][j]=219;
						printf("%c",A[i][j]);
					}
					else
						{
							A[i][j]=32;
							printf("%c",A[i][j]);
						}
					}
			}
		printf("\n");					
	}
}
struct toado
{
	int x;
	int y;
};
struct saochep
{
	int x;
	int y;
};
void inra(int x, int y, char *B)// in 1 diem ra man hinh
{
	gotoxy(x,y);
	cout<<B;
}
int huongbam(unsigned char &c,int &huong,int &huongcu)
{
	if(kbhit())
		{
			c=getch();
				if(c==224)
					{
						c=getch();
						if(c==75) huong  =1;
						if(c==72) huong  =2;
						if(c==77) huong  =3;
						if(c==80) huong  =4;
					}
		}
	if(((huongcu==3)&&(huong==1))||((huongcu==1)&&(huong==3)))
		return huongcu;
	if(((huongcu==2)&&(huong==4))||((huongcu==4)&&(huong==2)))
		return huongcu;
	return huong;
}
void thucan(int &x,int &y)
{
	srand((int)time(NULL));
	x=(rand()%77)+1;
	y=(rand()%22)+1;
	inra(x,y,"$");
	
}
int main()
{	
	unsigned char c;
	int A[28][80];
	taomap(A);
	int huong,huongcu=3;
	int diem=0,diemtruoc=0;				//diem so
	int m,n;				//toa do thuc an
	int dai=3;				// do dai ban dau cua con ran
	int u=0,v=0;			//danh dau 
	toado ran[70];			// thanh phan cua ran
	saochep sc[70];
	inra(ran[0].x=5,ran[0].y=10,"@");	// khoi tao ran ban dau
	inra(ran[1].x=4,ran[1].y=10,"*");
	inra(ran[2].x=3,ran[2].y=10,"*");
	for (int i = 3; i < 70; i++) ran[i].x = -1; 
	thucan(m,n);
	gotoxy(3,26);
	printf("Score : %d",diem);
	do
	{
			if(kbhit())
			{
					do
					{
						if(diem>diemtruoc)
						{
							gotoxy(3,26);
							printf("Score : %d",diem);
						}
					
						if((ran[0].x==0)||(ran[0].x==79))			// kiem tra va vao tuong
							{
								u++;
								break;
							}
						if((ran[0].y==0)||(ran[0].y==23))		// kiem tra va vao tuong
							{
								u++;
								break;
							}
						for(int i=0;i<dai;i++)      // kiem tra thuc an co trung ng ko , tao thuc an moi, tang chieu dai
						{
							if((m==ran[i].x)&&(n==ran[i].y))
							{
								thucan(m,n);	
								dai++;
								diemtruoc=diem;
								diem=diem+50;
							}
							else
								break;
						}
						for(int i=0;i<dai;i++)	//sao luu lai
						{
							sc[i].x=ran[i].x;	
							sc[i].y=ran[i].y;
						}
					
							Sleep(100);
							huongbam(c,huong,huongcu);
							huongcu=huongbam(c,huong,huongcu);
							if(huongbam(c,huong,huongcu)==1)	ran[0].x -=1;
							if(huongbam(c,huong,huongcu)==3)	ran[0].x +=1;
							if(huongbam(c,huong,huongcu)==2)	ran[0].y -=1;
							if(huongbam(c,huong,huongcu)==4)	ran[0].y +=1;
					
							inra(ran[dai - 1].x, ran[dai - 1].y, " "); // xoa duoi
					
							for(int i=1;i<dai;i++)	// nhan lai gia tri 
							{
								ran[i].x=sc[i-1].x;
								ran[i].y=sc[i-1].y;
								
							}
								
							for(int i=0;i<dai;i++)
							{
								if(i==0)	
									inra(ran[i].x,ran[i].y,"@");
								else
									inra(ran[i].x,ran[i].y,"*");
							}
						
						// kiem tra va cham
							for(int i=1;i<dai;i++)
							{
									if((ran[0].x==ran[i].x)&&(ran[0].y==ran[i].y))
									{
										v++;
										break;
									}
							}
							if(v!=0)
							{	
								u++;
								break;
							}
						
					}
					while(c!=27);
					
			}
	}
	while(u==0);
	system("cls");
	taomap(A);

	gotoxy(30,12);
	printf("GAME OVER");
	
	gotoxy(27,13);
	printf("YOUR SCORE : %d",diem);
	
	_getch();

}
